import request from "@/utils/request";

export default {
    name: "Login",
    data() {
        return {
            identity: "",
            numid:null,
            form: {
                username: null,
                password: "",
                identity: "",
            },
            rules: {
                username: [
                    {required: true, message: "请输入用户名", trigger: "blur"},{type:'number',message:'必须是数字',trigger:'blur'}
                ],
                password: [{required: true, message: "请输入密码", trigger: "blur"}],
                identity: [{required: true, message: "请选择身份", trigger: "blur"}],
            },
        };
    },
    computed: {
        disabled() {
            const {username, password, identity} = this.form;
            return Boolean(username && password && identity);
        },
    },
    methods: {
        login() {
           const _this=this;
            
            this.$refs.form.validate((valid) => {
               console.log(111)
                if (valid) {
                    _this.identity = this.form.identity;
                    var par={};
                    if(_this.identity=="student"){
                        par.stuPassword=this.form.password
                        par.stuId=this.form.username
                        _this.numid=0;
                    }
                    else if(_this.identity=="dromadmin"){
                        par.sgPassword=this.form.password
                        par.sgId=this.form.username
                        _this.numid=1;
                    }
                    else{
                        par.password=this.form.password
                        par.sysId=this.form.username
                        _this.numid=2;
                    }
                    console.log(par)
                    request.post("/" + this.identity + "/login", par).then((res) => {
                        if (res.code === "1") {
                            _this.$message({
                                message: "登陆成功",
                                type: 'success'
                              });
                            // 登陆成功跳转主页
                            window.localStorage.setItem("user", JSON.stringify(res.data));
                            window.localStorage.setItem("identity", _this.numid);
                            this.$router.replace({path: "/home"});
                        } else {
                            _this.$message({
                                message: res.msg,
                                type: 'error'
                              });
                        }
                    });
                   
                }
            });
        },
    },
};